<script type="text/javascript">
	// 初始化
	jQuery(function() {
		create_user_grid();
		jQuery("#frm input[type='text']").keydown(function(e) {
			var curKey = e.which;
			if (curKey == 13) {
				query_data();
			}
		});
	})

	// 创建grid
	function create_user_grid() {
		var sort = jQuery(".data_operate_bar [name='fieldSort']").val();
		gxc.grid.create({
			name : "user_grid",
			url : "$!{paramTool.url_local}/system/user/grid.do",
			withCheckbox : true,
			withRownum : false,
			stripeRows : true,
			stripeOverRow : false,
			withPagebar : true,
			pageSize : 10,
			renderTo : "#user_grid",
			fieldSort : sort,
			loadDataNow : true,
			columns : [ {
				text : "用户账号",
				name : "id"
			}, {
				text : "用户姓名",
				name : "name"
			}, {
				text : "创建时间",
				name : "create_time"
			}, {
				text : "操作",
				name : "id",
				width : "200px",
				className : "td_operate",
				align : "center",
				render : renderOperate
			} ]
		});
	}

	// grid操作列
	function renderOperate(item) {
		var operate = "<a  href='javascript: viewUser(\"" + item.id
				+ "\")'>查看</a>";
		operate += "<a style='margin-left:5px;' href='javascript: editUser(\""
				+ item.id + "\")'>编辑</a>";
		operate += "<a style='margin-left:5px;' href='javascript: deleteUser(\""
				+ item.id + "\")'>删除</a>";
		operate += "<a style='margin-left:5px;' href='javascript: listRole(\""
				+ item.id + "\")'>角色关联</a>";
		return operate;
	}

	// 查询
	function query_data() {
		gxc.grid.user_grid.params = null;
		gxc.grid.user_grid.params = gxc.form.getParams("#frm");
		gxc.grid.user_grid.pageCurrent = 1;
		gxc.grid.user_grid.reload();
	}

	// 刷新
	function reload() {
		gxc.grid.user_grid.reload();
	}

	// 删除选中记录
	function deleteSelected() {
		var id_array = gxc.grid.user_grid.getSelectedRows().toString();
		if (id_array == "") {
			gxc.dialog.prompt("请选择需要删除的记录！");
			return;
		} else {
			jQuery.ajax({
				type : "post",
				url : "$!{paramTool.url_local}/system/user/deleteBatch.do",
				data : {
					id : id_array
				},
				dataType : "json",
				beforeSend : function(XMLHttpRequest) {
				},
				success : function(data, textStatus) {
					if (data.status == "success") {
						gxc.dialog.prompt("成功删除 " + data.data + " 条记录！");
						gxc.grid.user_grid.reload();
					}
				},
				complete : function(XMLHttpRequest, textStatus) {
				},
				error : function() {
				}
			});
		}
	}

	// 删除单条记录
	function deleteUser(id) {
		jQuery.ajax({
			type : "post",
			url : "$!{paramTool.url_local}/system/user/delete.do",
			data : {
				id : id
			},
			dataType : "json",
			beforeSend : function(XMLHttpRequest) {
			},
			success : function(data, textStatus) {
				if (data.status == "success") {
					gxc.dialog.prompt("成功删除 " + data.data + " 条记录！");
					gxc.grid.user_grid.reload();
				}
			},
			complete : function(XMLHttpRequest, textStatus) {
			},
			error : function() {
			}
		})
	}

	// 更改排序
	function changeFieldSort() {
		gxc.grid.user_grid.fieldSort = jQuery("[name='fieldSort']").val();
		gxc.grid.user_grid.page_current = 1;
		gxc.grid.user_grid.reload();
	}

	// 显示或隐藏查询区域
	function toggleQuery() {
		var obj = jQuery(".query_area");
		jQuery(".query_area").slideToggle();
		if (jQuery("#toggleQueryArea").html() == "显示查询") {
			jQuery("#toggleQueryArea").html("隐藏查询");
		} else {
			jQuery("#toggleQueryArea").html("显示查询");
		}
	}

	// 重置查询表单
	function query_reset() {
		jQuery("#frm")[0].reset();
	}

	// 新建
	function createUser() {
		window.parent.createTab("system_user_add", "新建用户",
				"${paramTool.url_local}/system/user/create.do");
	}

	// 查看详细
	function viewUser(user_id) {
		window.parent.createTab("system_user_view" + user_id, "查看用户",
				"${paramTool.url_local}/system/user/view.do?id=" + user_id);
	}

	// 修改
	function editUser(user_id) {
		window.parent.createTab("system_user_edit" + user_id, "修改用户",
				"${paramTool.url_local}/system/user/edit.do?id=" + user_id);
	}

	// 关联角色
	function listRole(user_id) {
		window.parent.createTab("system_user_listRole" + user_id, "用户关联角色",
				"${paramTool.url_local}/system/user/listRole.do?user_id="
						+ user_id);
	}
</script>